package com.stylefeng.guns.modular.party.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.common.constant.dictmap.ConstitutionDict;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.party.service.IConstitutionService;
import com.stylefeng.guns.modular.system.model.Constitution;
import com.stylefeng.guns.modular.system.warpper.ConstitutionWrapper;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 宪法管理控制器
 *
 * @author fengshuonan
 * @Date 2018-11-26 02:22:44
 */
@Controller
@RequestMapping("/constitution")
public class ConstitutionController extends BaseController {

    private String PREFIX = "/party/constitution/";

    @Autowired
    private IConstitutionService constitutionService;

    /**
     * 跳转到宪法管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "constitution.html";
    }

    /**
     * 跳转到添加宪法管理
     */
    @RequestMapping("/constitution_add")
    public String constitutionAdd() {
        return PREFIX + "constitution_add.html";
    }

    /**
     * 跳转到修改宪法管理
     */
    @RequestMapping("/constitution_update/{constitutionId}")
    public String constitutionUpdate(@PathVariable Integer constitutionId, Model model) {
        Constitution constitution = constitutionService.selectById(constitutionId);
        model.addAttribute("constitution",constitution);
        LogObjectHolder.me().set(constitution);
        return PREFIX + "constitution_edit.html";
    }

    /**
     * 获取宪法管理列表
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value="获取宪法列表",notes="获取宪法列表,条件查询并分页")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="name",value="宪法名称",required=false,dataType="String",paramType="query"),
    	@ApiImplicitParam(name="beginTime",value="注册开始时间",required=false,dataType="String",paramType="query"),
    	@ApiImplicitParam(name="endTime",value="注册结束时间",required=false,dataType="String",paramType="query")
     }
    )
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, @RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime) {
    	//分页
    	Page<Constitution> page = new PageFactory<Constitution>().defaultPage();
    	List<Map<String,Object>> constitutionList = constitutionService.selectConstitutions(page,name,beginTime,endTime,page.getOrderByField(),page.isAsc());
        page.setRecords((List<Constitution>)new ConstitutionWrapper(constitutionList).warp());
    	return super.packForBT(page);
    }

    /**
     * 新增宪法管理
     */
    @ApiOperation(value="添加宪法管理",notes="添加宪法管理")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @BussinessLog(value = "添加宪法管理", key = "name", dict = ConstitutionDict.class)
    @ResponseBody
    public Object add(Constitution constitution) {
    	constitution.setCreatetime(new Date());
    	constitution.setSort(constitution.getSort().longValue());
        constitutionService.insert(constitution);
        return SUCCESS_TIP;
    }

    /**
     * 删除宪法管理
     */
    @ApiOperation(value="删除宪法管理",notes="删除宪法管理")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="id",value="宪法主键id",required=true,dataType="Integer",paramType="query"),
     }
    )
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @BussinessLog(value = "删除宪法管理", key = "constitutionId", dict = ConstitutionDict.class)
    @ResponseBody
    public Object delete(@RequestParam Long constitutionId) {
    	 //缓存名称
        //LogObjectHolder.me().set(ConstantFactory.me().getConstitutionName(constitutionId));
        constitutionService.deleteById(constitutionId);
        return SUCCESS_TIP;
    }

    /**
     * 修改宪法管理
     */
    @ApiOperation(value="修改宪法管理",notes="修改宪法管理")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @BussinessLog(value = "修改宪法管理", key = "id", dict = ConstitutionDict.class)
    @ResponseBody
    public Object update(Constitution constitution) {
    	if (ToolUtil.isOneEmpty(constitution, constitution.getId(), constitution.getName(), constitution.getContent())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
    	Constitution old = this.constitutionService.selectById(constitution.getId());
    	old.setContent(constitution.getContent());
    	old.setName(constitution.getName());
    	old.setCategory(constitution.getCategory());
    	old.setSort(constitution.getSort());
    	old.setStatus(constitution.getStatus());
    	old.setCreatetime(new Date());
    	old.updateById();
        return SUCCESS_TIP;
    }

    /**
     * 宪法管理详情
     */
    @ApiOperation(value="查看宪法管理",notes="查看宪法管理")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="constitutionId",value="宪法主键id",required=true,dataType="Integer",paramType="path"),
     }
    )
    @RequestMapping(value = "/detail/{constitutionId}",method = RequestMethod.POST)
    @ResponseBody
    public Object detail(@PathVariable("constitutionId") Long constitutionId) {
        return constitutionService.selectById(constitutionId);
    }
}
